package com.base.system.managedBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.base.security.entity.SecurityRole;
import com.base.security.entity.UserRole;
import com.base.security.service.SecurityEntityManager;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "system_editUserRoleBean", scope = ManagedBeanScope.REQUEST)
public class EditUserRoleBean implements Serializable {
	private SecurityEntityManager seManager = SpringBeanHolder
			.getBean("securityEntityManager");
	// 过滤后的角色
	private List<SelectItem> filteredRoles;
	// 用户已经拥有的角色
	private List<SelectItem> roleUsers;
	// 搜索词
	private String prefix;
	// 传进来的用户id
	@SaveState
	private String userId;
	@Inject
	private FacesContext context;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			userId = context.getExternalContext().getRequestParameterMap().get(
					"userId");
			roleUsers = this.getRolesOfUser();
			context.getExternalContext().getSessionMap().put("USERROLES",
					roleUsers);
		}
		roleUsers = (List<SelectItem>) context.getExternalContext()
				.getSessionMap().get("USERROLES");
		// System.out.println("得到的roleId是:" + roleId);
	}

	@Action(id = "saveUserRole")
	public void saveUserRole() {
		String selectedRoles = context.getExternalContext()
				.getRequestParameterMap().get("selectedRoles");
		seManager.mergeRolesOfUser(selectedRoles, userId);
		// 更新sessionMap中的值
		roleUsers = this.getRolesOfUser();
		context.getExternalContext().getSessionMap()
				.put("USERROLES", roleUsers);
		String script = "closeWindow('role_relation')";
		Browser.execClientScript("window.parent", script);
	}

	// 值改变后触发的事件
	public void prefixChanged(ValueChangeEvent event) {
		filteredRoles = buildFilteredRoles((String) event.getNewValue());
	}

	private List<SelectItem> buildFilteredRoles(String prefix) {
		// System.out.println("输入进来的值是：" + prefix);
		List<SelectItem> result = new ArrayList<SelectItem>();
		if (prefix != null)
			prefix = prefix.trim();
		List<SecurityRole> roles = new ArrayList<SecurityRole>();
		if ("".equals(prefix))
			roles = seManager.getAllSecurityRole();
		else
			roles = seManager.getRoleByDisplayName(prefix);
		// System.out.println("取到的user size:" + users.size());
		for (SecurityRole sr : roles) {
			result.add(new SelectItem(sr.getId(), sr.getDisplayName()));
		}
		return result;
	}

	// 得到全部角色
	private List<SelectItem> getAllItems() {
		List<SecurityRole> roles = seManager.getAllSecurityRole();
		List<SelectItem> items = new ArrayList<SelectItem>();
		for (SecurityRole sr : roles) {
			items.add(new SelectItem(sr.getId(), sr.getDisplayName()));
		}
		return items;
	}

	// 得到角色已经分配的用户
	private List<SelectItem> getRolesOfUser() {
		List<UserRole> users = seManager.getUserRoleOfUser(Integer
				.parseInt(userId));
		List<SelectItem> items = new ArrayList<SelectItem>();
		for (UserRole ur : users) {
			items.add(new SelectItem(ur.getRole().getId(), ur.getRole()
					.getDisplayName()));
		}
		return items;
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public void setFilteredRoles(List<SelectItem> filteredRoles) {
		this.filteredRoles = filteredRoles;
	}

	public List<SelectItem> getFilteredRoles() {
		if (filteredRoles == null)
			filteredRoles = this.getAllItems();
		return filteredRoles;
	}

	public List<SelectItem> getRoleUsers() {
		return roleUsers;
	}

	public void setRoleUsers(List<SelectItem> roleUsers) {
		this.roleUsers = roleUsers;
	}
}
